#include "lineEq.glsl"
#include "lineEqR.glsl"

in vec3 position;

float getLineX() { return getLineX(position.z); }
float getLineXR() { return getLineXR(position.z); }

struct Ellipse {
	bool isActive;
	float centerZ;
	float hAxis;
	float vAxis;
	bool isALCActive;
	bool isALCToLeft;
	vec3 colorALC;
	bool isLDWActive;
	bool isLDWFromLeft;
	vec3 colorLDW;
};
uniform Ellipse ellipse;

vec3 blend(float lineX, vec3 modelColor, vec3 ellipseColor) {
	float deltaX = abs(position.x - lineX) / ellipse.hAxis / 2.0;
	float deltaZ = abs(position.z - ellipse.centerZ) / ellipse.vAxis / 2.0;
	float fx = pow(deltaX, 0.3);
	float fz = smoothstep(0.0, 1.0, deltaZ);
	float mixFactor = (1.0 - fx) * (1.0 - fz);
	return mix(modelColor, ellipseColor, mixFactor);
}

vec3 attachEllipseEffect(vec3 color) {
	if (ellipse.isALCActive || ellipse.isLDWActive) {
		float lX = getLineX();
		float rX = getLineXR();

		if (ellipse.isALCActive) {
			if (ellipse.isALCToLeft && position.x < lX)
				color = blend(lX, color, ellipse.colorALC);
			else if (!ellipse.isALCToLeft && position.x > rX)
				color = blend(rX, color, ellipse.colorALC);
		}
		if (ellipse.isLDWActive) {
			if (ellipse.isLDWFromLeft && position.x > lX)
				color = blend(lX, color, ellipse.colorLDW);
			else if (!ellipse.isLDWFromLeft && position.x < rX)
				color = blend(rX, color, ellipse.colorLDW);
		}
	}
	return color;
}